package com.ruoyi.system.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.LabourProtectArticles;
import com.ruoyi.system.mapper.LabourProtectArticlesMapper;
import com.ruoyi.system.service.LabourProtectArticlesService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 劳动防护用品和装备(LabourProtectArticles)表服务实现类
 *
 * @author makejava
 * @since 2022-11-16 15:25:24
 */
@Service("labourProtectArticlesService")
public class LabourProtectArticlesServiceImpl implements LabourProtectArticlesService {
    @Resource
    private LabourProtectArticlesMapper labourProtectArticlesDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public LabourProtectArticles queryById(Integer id) {
        return this.labourProtectArticlesDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param labourProtectArticles 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<LabourProtectArticles> queryByPage(LabourProtectArticles labourProtectArticles, PageRequest pageRequest) {
        long total = this.labourProtectArticlesDao.count(labourProtectArticles);
        return new PageImpl<>(this.labourProtectArticlesDao.queryAllByLimit(labourProtectArticles, pageRequest), pageRequest, total);
    }

    @Override
    public List<LabourProtectArticles> selectList(LabourProtectArticles labourProtectArticles) {
        return labourProtectArticlesDao.queryAll(labourProtectArticles);
    }

    /**
     * 新增数据
     *
     * @param labourProtectArticles 实例对象
     * @return 实例对象
     */
    @Override
    public LabourProtectArticles insert(LabourProtectArticles labourProtectArticles) {
        this.labourProtectArticlesDao.insert(labourProtectArticles);
        return labourProtectArticles;
    }

    /**
     * 修改数据
     *
     * @param labourProtectArticles 实例对象
     * @return 实例对象
     */
    @Override
    public LabourProtectArticles update(LabourProtectArticles labourProtectArticles) {
        this.labourProtectArticlesDao.update(labourProtectArticles);
        return this.queryById(labourProtectArticles.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.labourProtectArticlesDao.deleteById(id) > 0;
    }

    @Override
    public String importExcel(List<LabourProtectArticles> labourProtectArticless, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(labourProtectArticless) || labourProtectArticless.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        labourProtectArticlesDao.insertBatch(labourProtectArticless);
        return "成功";
    }
}
